package com.itbour.web.action.authority;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.itbour.com.page.Page;
import com.itbour.common.base.BaseAction;
import com.itbour.common.util.StringUtils;
import com.itbour.dto.authority.BackFuncDto;
import com.itbour.dto.authority.RoleDto;
import com.itbour.dto.authority.RoleFuncDto;
import com.itbour.dto.com.UserInfoDto;
import com.itbour.service.authority.BackFuncInfoService;
import com.itbour.service.authority.RoleInfoService;
import com.opensymphony.xwork2.ActionContext;

/**
 * 控制角色信息ACTION
 * @author LQ
 */
public class GetRoleInfoWebAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	private RoleInfoService roleInfoService;
	private BackFuncInfoService backFuncInfoService;
	private RoleDto roleDto;
	private HashMap<String, Object> mapResult = new HashMap<String, Object>();
	private List<RoleDto> listResult;
	private List<BackFuncDto> listBackFunc;
	private String findDesc;// 通过描述内容查找
	private String findMeta;//通过用户角色分类
	private String findType;//通过分类
	private List<RoleFuncDto> fReusult;
	
	
	/**
	 * ==分页获取角色信息
	 * @return mapResult
	 * @throws Exception
	 */
	public String pageGetRoleInfo() throws Exception {
		Map<String, Object> session = ActionContext.getContext().getSession();
		UserInfoDto user = (UserInfoDto) session.get("user");
		if (user != null && user.getUser_id() != null) {
			Map<String, Object> parm = new HashMap<String, Object>();
			HttpServletRequest request = ServletActionContext.getRequest();
			Integer pageNum = Integer.parseInt(request.getParameter("page"));
			Integer pageSize = Integer.parseInt(request.getParameter("rows"));
			if (StringUtils.isNotEmpty(findDesc)) {
				parm.put("desc", findDesc);
			}
			if(StringUtils.isNotEmpty(findMeta)){
				parm.put("role_meta", findMeta);
			}
			if(StringUtils.isNotEmpty(findType)){
				parm.put("role_type", findType);
			}
			Page page = roleInfoService.pageListRoleInfo(pageNum, pageSize, parm);
			mapResult.put("total", page.getTotalCount());
			mapResult.put("rows", page.getResult());
		} else {
			this.writeJSON("no");
		}
		return SUCCESS;
	}

	/*** 添加角色信息
	 * @throws Exception
	 */
	public void addRoleInfo() throws Exception {
		if (roleDto != null) {
			Map<String, Object> session = ActionContext.getContext().getSession();
			UserInfoDto user = (UserInfoDto) session.get("user");
			if (user != null && user.getUser_id() != null) {
					roleDto.setUpdate_by(user.getUser_id().toString());
					if (roleDto.getRole_id() != null) {
						this.updRoleInfo();// 修改信息
					} else {
						roleInfoService.addRoleInfo(roleDto);
						this.writeJSON("ok");
					}
			 }else {
				this.writeJSON("no");
			 }
		} else {
			this.writeJSON("no");
		}
	}

	/**
	 * 更新修改角色信息
	 * @throws Exception
	 */
	public void updRoleInfo() throws Exception {
		if (roleDto != null) {
			Map<String, Object> session = ActionContext.getContext().getSession();
			UserInfoDto user = (UserInfoDto) session.get("user");
			if (user != null && user.getUser_id() != null&&roleDto.getRole_id()!=null) {
				roleDto.setUpdate_by(user.getUser_id().toString());
				roleInfoService.updRoleInfo(roleDto);
				this.writeJSON("ok");
			} else {
				this.writeJSON("no");
			}
		} else {
			this.writeJSON("no");
		}
	}

	/**
	 * 获取单个角色信息 用于修改
	 */
	public String getOneRoleInfo() {
		HttpServletRequest request = ServletActionContext.getRequest();
		Integer id = Integer.parseInt(request.getParameter("id"));
		if (id != null) {
			Map<String, Object> parm = new HashMap<String, Object>();
			parm.put("role_id", id);
			listResult = roleInfoService.selectOneRoleInfo(parm);
		}
		return SUCCESS;
	}

	/**
	 * 删除角色信息
	 * @throws Exception
	 */
	public void delRoleInfo() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		Map<String, Object> session = ActionContext.getContext().getSession();
		UserInfoDto user = (UserInfoDto) session.get("user");
		String ids = request.getParameter("ids");
		if (ids != null && user.getUser_id() != null && ids.length() > 0) {
			Map<String, Object> parm = new HashMap<String, Object>();
			parm.put("ids",StringUtils.listToString2(StringUtils.stringToList2(ids)));
			parm.put("update_by", user.getUser_id());
			roleInfoService.delRoleInfo(parm);
			this.writeJSON("ok");
		}
	}
	
	
	/**确认授权
	 * @throws Exception
	 */
	public void sureEmpower() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		Map<String, Object> session = ActionContext.getContext().getSession();
		UserInfoDto user = (UserInfoDto) session.get("user");
		String ids = request.getParameter("checkId");
		String roleId=request.getParameter("roleId");
		String fType = request.getParameter("fType");// 功能分类
		String authority2=request.getParameter("authority2");
		String strList1 = null;
		String strList2 = null;
		String strList3 = null;
		if(authority2!=null){
			List<String> list1=StringUtils.stringToList(ids);
			List<String> list2=StringUtils.stringToList(authority2);
			list1.retainAll(list2);//交集
			if(list1.size()>0){
				strList1=StringUtils.listToString(list1);
			}else{
				strList2=StringUtils.listToString(list2);//交集为空 直接设置权限2
			}
			List<String> list3=StringUtils.stringToList(ids);
			List<String> list4=StringUtils.stringToList(authority2);
			list3.removeAll(list4);//补集
			strList3=StringUtils.listToString(list3);
		}
		if (user.getUser_id() != null && StringUtils.isNotEmpty(fType)) {
			Map<String, Object> parm = new HashMap<String, Object>();
			parm.put("roleId", roleId);
			parm.put("strList3", strList3);// 补集
			parm.put("strList1", strList1);// 交集
			parm.put("strList2", strList2);// 交集为空 直接设置权限2
			parm.put("update_by", user.getUser_id());
			parm.put("func_type", fType);
			boolean b = roleInfoService.sureEmpower(parm);
			if (b){
				this.writeJSON("ok");
			} else {
				this.writeJSON("nc");// 不用操作
			}
		} else {
			this.writeJSON("ng");
		}
	}
	
	
	/**获取 不同父类 下的功能列表
	 */
	public String getFatherFuncInfo() {
		Map<String, Object> session = ActionContext.getContext().getSession();
		UserInfoDto user = (UserInfoDto) session.get("user");
		if (user != null && user.getUser_id() != null) {
			HttpServletRequest request=ServletActionContext.getRequest();
			Integer roleId=Integer.parseInt(request.getParameter("roleId"));
			String fType = request.getParameter("findFtype");// 功能分类
			Map<String, Object> parm=new HashMap<String, Object>();
			parm.put("role_id", roleId);
			parm.put("func_type",fType);
			List<RoleFuncDto> temp=new ArrayList<RoleFuncDto>();
			List<RoleFuncDto> temp2=new ArrayList<RoleFuncDto>();
			temp=roleInfoService.getFatherFunc(parm);
			temp2=roleInfoService.getTopInfo(parm);
			temp2.addAll(temp);
			Collections.sort(temp2);
			fReusult=temp2;
		}
		return SUCCESS;
	}
	
	

	public HashMap<String, Object> getMapResult() {
		return mapResult;
	}

	public void setMapResult(HashMap<String, Object> mapResult) {
		this.mapResult = mapResult;
	}
	public RoleInfoService getRoleInfoService() {
		return roleInfoService;
	}

	public void setRoleInfoService(RoleInfoService roleInfoService) {
		this.roleInfoService = roleInfoService;
	}

	public RoleDto getRoleDto() {
		return roleDto;
	}

	public void setRoleDto(RoleDto roleDto) {
		this.roleDto = roleDto;
	}

	public List<RoleDto> getListResult() {
		return listResult;
	}

	public void setListResult(List<RoleDto> listResult) {
		this.listResult = listResult;
	}

	public String getFindDesc() {
		return findDesc;
	}

	public void setFindDesc(String findDesc) {
		this.findDesc = findDesc;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public String getFindMeta() {
		return findMeta;
	}

	public void setFindMeta(String findMeta) {
		this.findMeta = findMeta;
	}

	public String getFindType() {
		return findType;
	}

	public void setFindType(String findType) {
		this.findType = findType;
	}

	public BackFuncInfoService getBackFuncInfoService() {
		return backFuncInfoService;
	}

	public void setBackFuncInfoService(BackFuncInfoService backFuncInfoService) {
		this.backFuncInfoService = backFuncInfoService;
	}

	public List<BackFuncDto> getListBackFunc() {
		return listBackFunc;
	}

	public void setListBackFunc(List<BackFuncDto> listBackFunc) {
		this.listBackFunc = listBackFunc;
	}

	public List<RoleFuncDto> getfReusult() {
		return fReusult;
	}

	public void setfReusult(List<RoleFuncDto> fReusult) {
		this.fReusult = fReusult;
	}
	
}
